package org.springframework.remoting.jsonrpc;

import java.io.IOException;
import java.io.Writer;

public class SpringJSAjaxHandler implements AJAXHandler {
  public void writeAjaxCall(Writer writer) throws IOException {
    //writer.write(" dojo.require(\"dojo.io.*\");\n");
    writer.write("\n");
    writer.write("\n");
    writer.write("var AjaxCall = Class.create();\n");
    writer.write("AjaxCall.prototype =\n");
    writer.write("{\n");
    writer.write("   makeCall :function()\n");
    writer.write("   {\n");
    writer.write("   return dojo.rawXhrPost({\n");
    writer.write("     url: this.url,\n");
    writer.write("         sync : this.options.sync,\n");
    writer.write("         postData : this.options.postContent,\n");
    writer.write("         contentType: \"text/json\",\n");
    writer.write("         load: this.options.onSuccess,\n");
    writer.write("         // if any error occurs, it goes here:\n");
    writer.write("         error: function(error,args){\n");
    writer.write("           console.warn(\"error!\",error);\n");
    writer.write("         }\n");
    writer.write("       });\n");
    writer.write("   },\n");
    writer.write("   setBody : function(json)\n");
    writer.write("   {\n");
    writer.write("     this.options.postContent = json;\n");
    writer.write("   }\n");
    writer.write(" };\n");
    writer.write(" Object.extend(AjaxCall.prototype, AjaxInterface.prototype);\n");
  }

  public void writeToJson(Writer writer) throws IOException {
    writer.write(" function toJSON(obj){\n");
    writer.write("  return dojo.toJson(obj);");
    writer.write("}\n");
    
  }
  
  
}
